package com.lxq.LeetCode.swapPairs;

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution {
    public static void main(String[] args) {

    }

    public static ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        } else {
            ListNode p1 = head, p2 = head.next, p3 = null;
            head = head.next;
            while (p2 != null) {
                p1.next = p2.next;
                p2.next = p1;
                if (p3 != null) {
                    p3.next = p2;
                }
                if (p1.next != null) {
                    p3 = p1;
                    p1 = p1.next;
                    p2 = p1.next;
                }else {
                    p2 = null;
                }
            }
        }

        return head;
    }

}
